package com.test001.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.test001.dao.Dao;
import com.test001.util.HibernateUtil;



public class BaseDaoImpl<T> implements Dao<T> {
	private Class<?> clazz = null;
	//直接传入实体类的class   属于第一种方法
	public BaseDaoImpl(Class<?> clazz){
		this.clazz = clazz;
	}
	
	//利用java反射，获取实际class
	public BaseDaoImpl(){
        //利用泛型的反射在实例化时拿到实体类的字节码
        Type t = this.getClass().getGenericSuperclass();
        ParameterizedType pt =  (ParameterizedType)t;
        Type actualType = pt.getActualTypeArguments()[0];
        clazz = (Class<?>)actualType;
    }	
	private SessionFactory sf = null;
	private Session session = null;
	public void save(T t) {
		sf = HibernateUtil.getSessionFactory();
		if(sf!=null){
			session  = sf.openSession();
			session.beginTransaction();
			session.save(t);
			session.getTransaction().commit();
			session.close();
		}else{
			System.out.println("数据库连接失败");
		}
	}

	public void delete(T t) {
		// TODO Auto-generated method stub
		
	}

	public void update(T t) {
		// TODO Auto-generated method stub
		
	}

	public List<T> getAll() {
		// TODO Auto-generated method stub
		return null;
	}


}
